//
//  APIUtil.m
//  YoucApi
//
//  Created by Liam on 12-10-3.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "APIUtil.h"
#import "CmnFunc.h"
#import "Reachability.h"
#import <CommonCrypto/CommonHMAC.h>
#import <CommonCrypto/CommonCryptor.h>
#import "NSData-Hex.h"
#import "YoucApiKeys.h"
#import "GDataDefines.h"
#import <CoreTelephony/CTCarrier.h>
#import <CoreTelephony/CTTelephonyNetworkInfo.h>
#import "Reachability.h"
#import "YAObjGame.h"
#import "YoucApiTask.h"

@implementation APIUtil

+ (NSString *)hmac_sha1:(NSData *)data withKey:(NSData *)key
{
	char cHMAC[CC_SHA1_DIGEST_LENGTH];
	
	CCHmac(kCCHmacAlgSHA1, [key bytes], [key length], [data bytes], [data length], cHMAC);
	
	NSData * HMAC = [[NSData alloc] initWithBytes:cHMAC length:CC_SHA1_DIGEST_LENGTH];
	
	NSString * hash = [HMAC toHexString];
	[HMAC release];
	return hash;
}

+ (NSString *)GMTString:(NSDate *)date
{
	NSDateFormatter * df = [[[NSDateFormatter alloc] init] autorelease];
	df.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
	df.dateFormat = @"EEE',' dd MMM yyyy HH':'mm':'ss 'GMT'";
	[df setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"] autorelease]];
	return [df stringFromDate:date];
}

+ (NSDate*)dateFromString:(NSString*)string
{
	NSDateFormatter * df = [[[NSDateFormatter alloc] init] autorelease];
	df.dateFormat = @"yyyy-MM-dd HH:mm:ss";
	[df setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"] autorelease]];
	NSDate *date = [df dateFromString:string];
	
	return date;
}

+ (NSString*)stringFromDate:(NSDate*)date
{
	NSDateFormatter * df = [[[NSDateFormatter alloc] init] autorelease];
	df.dateFormat = @"yyyy-MM-dd HH:mm:ss";
	[df setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"] autorelease]];
	return [df stringFromDate:date];
}

+ (BOOL)checkCanUseNetwork
{
	BOOL isEnableNetwork = [[Reachability reachabilityForInternetConnection] currentReachabilityStatus] != NotReachable;
	
	return isEnableNetwork;
}

//static CARespUserInfo *g_userInfo = nil;
//static CARespUserInfoExt *g_userInfoExt = nil;
//static NSString *g_sessionKey = nil;
static NSString *g_sessionSecret = nil;
static NSInteger g_keepAlive = -1;

//+ (void)setUserInfo:(CARespUserInfo *)userInfo
//{
//	@synchronized([APIUtil class])
//	{
//		SAFE_RELEASE(g_userInfo);
//		g_userInfo = [userInfo retain];
//	}
//}
//
//+ (void)setUserInfoExt:(CARespUserInfoExt *)userInfoExt
//{
//	@synchronized([APIUtil class])
//	{
//		SAFE_RELEASE(g_userInfoExt);
//		g_userInfoExt = [userInfoExt retain];
//	}
//}
//
//+ (CARespUserInfo *)getUserInfo
//{
//	@synchronized([APIUtil class])
//	{
//		return g_userInfo;
//	}
//}
//
//+ (CARespUserInfoExt *)getUserInfoExt
//{
//	@synchronized([APIUtil class])
//	{
//		return g_userInfoExt;
//	}
//}

//+ (void)setSessionKey:(NSString*)sessionKey
//{
//	@synchronized([APIUtil class])
//	{
//		SAFE_RELEASE(g_sessionKey);
//		g_sessionKey = [sessionKey retain];
//	}
//}
//
//+ (NSString*)getSessionKey
//{
//	@synchronized([APIUtil class])
//	{
//		return g_sessionKey;
//	}
//}

+ (void)setSessionSecret:(NSString*)sessionSecret
{
	@synchronized([APIUtil class])
	{
		SAFE_RELEASE(g_sessionSecret);
		g_sessionSecret = [sessionSecret retain];
	}
}

+ (NSString*)getSessionSecret
{
	@synchronized([APIUtil class])
	{
		return g_sessionSecret;
	}
}

+ (void)keepUserSession
{
	//[[YoucApi instance] keepUserSession:nil];
}

+ (void)setKeepAlive:(NSInteger)keepAlive
{
	@synchronized([APIUtil class])
	{
		if (keepAlive > 0)
		{
			g_keepAlive = keepAlive * 3 / 4;
			[[APIUtil class] performSelectorOnMainThread:@selector(keepUserSession) withObject:nil waitUntilDone:NO];
		}
		else
		{
			g_keepAlive = 0;
		}
	}
}

+ (NSInteger)getKeepAlive
{
	@synchronized([APIUtil class])
	{
		return g_keepAlive;
	}
}

//+ (NSString*)getClientType
//{
//	if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
//	{
//		return @"TELEIPAD";
//	}
//	else
//	{
//		return @"TELEIPHONE";
//	}
//}

+ (NSString *)getNetworkAccessMode
{
	BOOL isEnableWIFI = ([[Reachability reachabilityForLocalWiFi] currentReachabilityStatus] == ReachableViaWiFi);
	
	return isEnableWIFI ? @"WIFI" : @"MOBILE";
}

+ (NSString *)getTelecomsOperator
{
	CTTelephonyNetworkInfo * info = [[[CTTelephonyNetworkInfo alloc] init] autorelease];
	NSString * name = info.subscriberCellularProvider.carrierName;
	NSLog(@"%@", [info.subscriberCellularProvider description]);
	return name ? name : @"";
}

+ (NSString*)getAppVersion
{
	return [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];
}

+ (NSString*)getAPIServerUrl
{
	NSString *serverUrl = [YoucApi instance].serverUrl;
	if (serverUrl == nil || [serverUrl length] == 0)
	{
		serverUrl = @"http://api.youc.com";
	}
	return serverUrl;
}

@end
